home *** CD-ROM | disk | FTP | other *** search
/ Aminet 51 / Aminet 51 (2002)(GTI - Schatztruhe)[!][Oct 2002].iso / Aminet / dev / c / minigl.lha / MiniGL / demos / warp.c < prev   
Encoding:
C/C++ Source or Header  |  2002-03-27  |  7.4 KB  |  361 lines

  1. /*
  2.     warp.c
  3.  
  4.     This is an example of what extreme field of view can do.....
  5.  
  6. */
  7. #include <mgl/gl.h>
  8. #include <math.h>
  9.  
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>    //OF
  13.  
  14. #ifdef __VBCC__
  15. #include <extra.h>    //OF
  16. #endif
  17.  
  18.  
  19. #ifndef M_PI
  20. #define M_PI 3.14159265
  21. #endif
  22.  
  23. GLint width=640; GLint height=480;
  24. GLfloat startilt = 0.0;
  25. GLfloat t_off = 0.0;
  26. GLfloat fov = 170.0;
  27.  
  28. typedef struct
  29. {
  30.     GLfloat x,y,z,u,v;
  31. } MyVertex;
  32.  
  33. GLenum LockMode = MGL_LOCK_SMART;
  34.  
  35. GLubyte *LoadPPM(char *name, GLint *w, GLint *h)
  36. {
  37.     int i;
  38.     unsigned long x,y;
  39.     FILE *f;
  40.     GLubyte *where;
  41.  
  42.     f = fopen(name, "r");
  43.  
  44.     if (!f)
  45.     {
  46.         *w = 0; *h=0;
  47.         return NULL;
  48.     }
  49.     #ifndef __STORM__
  50.     i = fscanf(f, "P6\n%lu %lu\n255\n",&x, &y);    //OF (%lu)
  51.     #else
  52.     i = fscanf(f, "P6\n%lu\n%lu\n255\n", &x, &y);    //OF (%lu)
  53.     #endif
  54.  
  55.     if (i!= 2)
  56.     {
  57.         printf("Error scanning PPM header\n");
  58.         fclose(f);
  59.         *w = 0; *h = 0;
  60.         return NULL;
  61.     }
  62.  
  63.     *w = x;
  64.     *h = y;
  65.  
  66.     where = malloc(x*y*3);
  67.     if (!where)
  68.     {
  69.         printf("Error out of Memory\n");
  70.         fclose(f);
  71.         *w = 0; *h = 0;
  72.         return NULL;
  73.     }
  74.  
  75.     i = fread(where, 1, x*y*3, f);
  76.     fclose(f);
  77.  
  78.     if (i != x*y*3)
  79.     {
  80.         printf("Error while reading file\n");
  81.         free(where);
  82.         *w = 0; *h = 0;
  83.         return NULL;
  84.     }
  85.  
  86.     return where;
  87. }
  88.  
  89.  
  90. BOOL TexInit(char *name, int num)
  91. {
  92.     GLubyte *tmap;
  93.     GLint x,y;
  94.  
  95.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  96.     glPixelStorei(GL_PACK_ALIGNMENT, 1);
  97.  
  98.     if (!name)
  99.     {
  100.         return FALSE;
  101.     }
  102.     else
  103.     {
  104.         tmap = LoadPPM(name, &x, &y);
  105.     }
  106.  
  107.     if (!tmap)
  108.         return FALSE;
  109.  
  110.     glBindTexture(GL_TEXTURE_2D, num);
  111.     glTexImage2D(GL_TEXTURE_2D, 0, 3,
  112.         x,y, 0, GL_RGB, GL_UNSIGNED_BYTE, tmap);
  113.     free(tmap);
  114.  
  115.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  116.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  117.  
  118.     glEnable(GL_TEXTURE_2D);
  119.  
  120.     return TRUE;
  121. }
  122.  
  123. static void drawFlare(GLint texnum)
  124. {
  125.     GLfloat x,y,w,h;
  126.  
  127.     w=width/10.0;
  128.     h=w;
  129.  
  130.     x=width/2.0-w/2.0;
  131.     y=height/2.0-h/2.0;
  132.  
  133.  
  134.     glBindTexture(GL_TEXTURE_2D, texnum);
  135.     glEnable(GL_BLEND);
  136.     glBlendFunc(GL_ONE, GL_ONE);
  137.     /*
  138.     ** An MGL_FLATFAN is a shortcut to draw something into the
  139.     ** screen that does not go through the transformation pipeline.
  140.     ** This is also not clipped, so you have to take care to avoid
  141.     ** going over the edges, or clip yourself.
  142.  
  143.  
  144.     */
  145.     glBegin(MGL_FLATFAN);
  146.         glTexCoord2f(0.0, 0.0);
  147.         glVertex2f(x,y);
  148.         glTexCoord2f(1.0, 0.0);
  149.         glVertex2f(x+w, y);
  150.         glTexCoord2f(1.0, 1.0);
  151.         glVertex2f(x+w,y+h);
  152.         glTexCoord2f(0.0, 1.0);
  153.         glVertex2f(x,y+h);
  154.     glEnd();
  155.     glDisable(GL_BLEND);
  156. }
  157.  
  158.  
  159. static void drawStarField(GLint tex1, GLfloat off)
  160. {
  161.     GLfloat w = 1.0;
  162.     #define BACKPLANE -8.0
  163.     #define TEXTU 0.0
  164.     #define TEXTV 2.0
  165.  
  166.     glBindTexture(GL_TEXTURE_2D, tex1);
  167.     glBegin(GL_QUADS);
  168.  
  169.         glTexCoord2f(TEXTU, TEXTV+off);
  170.         glVertex3f(-1.0,  1.0,  BACKPLANE);
  171.         glTexCoord2f(TEXTU, off);
  172.         glVertex3f(-1.0,  1.0,   1.0);
  173.         glTexCoord2f(1.0, off);
  174.         glVertex3f(-1.0, -1.0,   1.0);
  175.         glTexCoord2f(1.0, TEXTV+off);
  176.         glVertex3f(-1.0, -1.0,  BACKPLANE);
  177.  
  178.         glTexCoord2f(TEXTU, TEXTV+off);
  179.         glVertex3f( 1.0,  1.0,  BACKPLANE);
  180.         glTexCoord2f(TEXTU, off);
  181.         glVertex3f( 1.0,  1.0,   1.0);
  182.         glTexCoord2f(1.0, off);
  183.         glVertex3f( 1.0, -1.0,   1.0);
  184.         glTexCoord2f(1.0, TEXTV+off);
  185.         glVertex3f( 1.0, -1.0,  BACKPLANE);
  186.  
  187.         glTexCoord2f(TEXTU, TEXTV+off);
  188.         glVertex3f(-1.0,  1.0,  BACKPLANE);
  189.         glTexCoord2f(TEXTU, off);
  190.         glVertex3f(-1.0,  1.0,   1.0);
  191.         glTexCoord2f(1.0, off);
  192.         glVertex3f( 1.0,  1.0,   1.0);
  193.         glTexCoord2f(1.0, TEXTV+off);
  194.         glVertex3f( 1.0,  1.0,  BACKPLANE);
  195.  
  196.         glTexCoord2f(TEXTU, TEXTV+off);
  197.         glVertex3f(-1.0, -1.0,  BACKPLANE);
  198.         glTexCoord2f(TEXTU, off);
  199.         glVertex3f(-1.0, -1.0,   1.0);
  200.         glTexCoord2f(1.0, off);
  201.         glVertex3f( 1.0, -1.0,   1.0);
  202.         glTexCoord2f(1.0, TEXTV+off);
  203.         glVertex3f( 1.0, -1.0,  BACKPLANE);
  204.  
  205.     glEnd();
  206. }
  207.  
  208.  
  209. void reshape(int w, int h)
  210. {
  211.     glMatrixMode(GL_PROJECTION);
  212.     glLoadIdentity();
  213.     gluPerspective(70.0, 1.3333333, 1.0, 100.0);
  214.  
  215.     glMatrixMode(GL_MODELVIEW);
  216.     glViewport(0, 0, (GLint)w, (GLint)h);
  217.     glClearColor(0.0f, 0.f, 0.f, 1.f);
  218.     glClearDepth(1.0);
  219. }
  220.  
  221. void DoFrame(void)
  222. {
  223.  if (LockMode == MGL_LOCK_MANUAL)
  224.     mglLockDisplay(); //needed with manual locking
  225.  
  226.     glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
  227.  
  228.     glMatrixMode(GL_PROJECTION);
  229.     glLoadIdentity();
  230.     gluPerspective(fov, 1.3333333, 1.0, 100.0);
  231.  
  232.     glMatrixMode(GL_MODELVIEW);
  233.     glLoadIdentity();
  234.  
  235.     glScalef(20.0, 20.0, 20.0);
  236.     glRotatef(startilt, 0.0, 0.0, 1.0);
  237.     glDisable(GL_CULL_FACE);
  238.  
  239.     drawStarField(1, t_off);
  240.     drawFlare(2);
  241.  
  242.     startilt += 0.1;
  243.     t_off += 0.007;
  244.  
  245. mglSwitchDisplay();
  246. }
  247.  
  248.  
  249. void keys(char c)
  250. {
  251.  
  252.     switch (c)
  253.     {
  254.         case 0x1b:
  255.             mglExit();
  256.             break;
  257.         case '+':
  258.             if (fov < 165.0)
  259.                 fov += 5.0;
  260.             break;
  261.         case '-':
  262.             if (fov > 30)
  263.                 fov -= 5.0;
  264.             break;
  265.     }
  266.  
  267. }
  268.  
  269. int main(int argc, char *argv[])
  270. {
  271.     int i;
  272.     char *filename = "data/stars.ppm";
  273.     char *flarename = "data/flare.ppm";
  274.  
  275.     for (i=1; i<argc; i++)
  276.     {
  277.         if (0 == stricmp(argv[i], "-width"))
  278.         {
  279.             i++;
  280.             width = atoi(argv[i]);
  281.         }
  282.         if (0 == stricmp(argv[i], "-height"))
  283.         {
  284.             i++;
  285.             height = atoi(argv[i]);
  286.         }
  287.         if (0 == stricmp(argv[i], "-window"))
  288.         {
  289.             mglChooseWindowMode(GL_TRUE);
  290.         }
  291.         if (0 == stricmp(argv[i], "-texture"))
  292.         {
  293.             i++;
  294.             filename = argv[i];
  295.         }
  296.         if (0 == stricmp(argv[i], "-flare"))
  297.         {
  298.             i++;
  299.             flarename = argv[i];
  300.         }
  301.   
  302.         if (0 == strcmp(argv[i], "-lock"))
  303.         {
  304.             i++;
  305.             if (0 == stricmp(argv[i], "manual"))
  306.             {
  307.                 LockMode = MGL_LOCK_MANUAL;
  308.             }
  309.             else if (0 == stricmp(argv[i], "auto"))
  310.             {
  311.                 LockMode = MGL_LOCK_AUTOMATIC;
  312.             }
  313.             else if (0 == stricmp(argv[i], "smart"))
  314.             {
  315.                 LockMode = MGL_LOCK_SMART;
  316.             }
  317.             else printf("Unknown lockmode. Using default\n");
  318.       }
  319.     }
  320.  
  321.     mglChooseVertexBufferSize(1000);
  322.     mglChooseNumberOfBuffers(3);
  323.  
  324.     MGLInit();
  325.  
  326.     mglCreateContext(0,0,width,height);
  327.     mglEnableSync(GL_TRUE);
  328.  
  329.     glClearColor(0.0, 0.0, 0.0, 0.0);
  330.     reshape(width, height);
  331.     glDisable(GL_DEPTH_TEST);
  332.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  333.  
  334.  
  335.     if ( TexInit(filename, 1) && TexInit(flarename, 2) )
  336.     {
  337.         glClearColor(0.0, 0.0, 0.0, 1.0);
  338.         glColor3f(1.0, 1.0, 1.0);
  339.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  340.  
  341.         glHint(MGL_W_ONE_HINT, GL_FASTEST);
  342.         glEnable(GL_TEXTURE_2D);
  343.         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  344.  
  345.  
  346.         mglLockMode(LockMode); //was MGL_LOCK_SMART
  347.         mglIdleFunc(DoFrame);
  348.         mglKeyFunc(keys);
  349.         mglMainLoop();
  350.     }
  351.     else
  352.     {
  353.         printf("Can't load textures\n");
  354.     }
  355.  
  356.     mglDeleteContext();
  357.     MGLTerm();
  358.     return 0;             /* ANSI C requires main to return int. */
  359. }
  360.  
  361.